package com.juc.concurrency.basic.sleep;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author huangweiyue
 * @description Thread.sleep会让出CPU的执行权，不参与调度，但是【锁并不会释放】
 * @return 不是A就是B，不会出现A,B交叉输出的情况，因为锁并不会释放
 * @date Created in 2021-07-30
 */
public class SleepExample {
    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {

        Thread threadA = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("child thread A is in sleep");
                Thread.sleep(3000);
                System.out.println("child thread A is in awaked");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        });

        Thread threadB = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("child thread B is in sleep");
                Thread.sleep(3000);
                System.out.println("child thread B is in awaked");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        });

        threadA.start();
        threadB.start();

    }

}
